package com.zhixun.zhlq.controller;


import com.zhixun.zhlq.utils.BsResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/fileUpload")
@Slf4j
public class FileUpload {

    @Value("${file.upload.fileDir}")
    private String uploadFilePath;

    @PostMapping("/uploadPic")
    public BsResult uploadPic(MultipartFile[] uploadFiles, HttpServletRequest request){
        String[] allowUploadFileType = {".bmp", ".gif", ".jpg", ".jpeg", ".webp", ".png"};
        return uploadFile(uploadFiles,request,allowUploadFileType,"images");
    }
    @PostMapping("/uploadMusic")
    public BsResult uploadMusic(MultipartFile[] uploadFiles, HttpServletRequest request){
        String[] allowUploadFileType = {".mp3", ".cda"};
        return uploadFile(uploadFiles,request,allowUploadFileType,"music");
    }
    @PostMapping("/uploadVideo")
    public BsResult uploadVideo(MultipartFile[] uploadFiles, HttpServletRequest request){
        String[] allowUploadFileType = {".mp4"};
        return uploadFile(uploadFiles,request,allowUploadFileType,"video");
    }


    @PostMapping("/uploadFile")
    public BsResult uploadFile(MultipartFile[] uploadFiles, HttpServletRequest request,String[] allowUploadFileType,String path){
        String fileUrl = "";//存储生成的访问路径
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
        if (uploadFiles.length > 0) {
            for (int i = 0; i < uploadFiles.length; i++) {
                MultipartFile uploadFile = uploadFiles[i];
                String oldName = uploadFile.getOriginalFilename();
                String end = oldName.substring(oldName.lastIndexOf("."));
                if (!Arrays.asList(allowUploadFileType).contains(end)){
                    return BsResult.success("请上传正确的文件类型！");
                }
                //根据上传的文件日期，进行分类保存
                String format = sdf.format(new Date());
                path = (path==null)?"file":path;
                File folder = new File(uploadFilePath+path+"/" +  format);
                if (!folder.isDirectory()) {
                    folder.mkdirs();
                }
                String newName = UUID.randomUUID().toString() + end;
                try {
                    //保存文件
                    uploadFile.transferTo(new File(folder, newName));
                    //生成上传文件的访问路径
                    String filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +  "/"+path+"/" + format + newName;
                    fileUrl = fileUrl + filePath + ",";
                } catch (IOException e) {
                    return BsResult.error(500, e.getMessage());
                }
            }
            fileUrl = fileUrl.substring(0,fileUrl.length() - 1);
            return BsResult.success("上传成功!").put("fileUrl", fileUrl);
        } else if (uploadFiles.length == 0) {
            return BsResult.error("请选择文件!");
        }
        return BsResult.error("上传文件失败!");
    }


}
